//
//  WZHomeController.swift
//  weibo
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking

let WZHomeViewControllerReuserIdenfiter = "CZHomeViewControllerReuserIdenfiter"

class WZHomeController: WZTableViewController {
    
//    
    var downLoadViewModel: WZDownLoadViewModel?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        if WZUserViewModel.shareInstance.isLogin{
            
            changeRootView()
            //        MARK:将下拉刷新的view添加到tableview上
            tableView.addSubview(refreshView)
            //        为下拉刷新view添加事件
        
    
        refreshView.addTarget(self, action: Selector.init("loadData"), forControlEvents: UIControlEvents.ValueChanged)
        }
    }
    //    MARK:  设置显示加载信息数量的LAble
    func showMessageLable(count:Int){
        var messageLable = UILabel()
       
//        设置信息lable的位置
        messageLable.frame = CGRectMake(0, 64-44, UIScreen.mainScreen().bounds.width, 44)
        self.navigationController?.view.insertSubview(messageLable, belowSubview: (navigationController?.navigationBar)!)
//        设置lable 的属性
         messageLable.backgroundColor = UIColor.greenColor()
        
        if count == 0{
            messageLable.text = "暂无刷新"
            
        }else{
            messageLable.text = "更新\(count)条信息"
        }
        messageLable.textAlignment = .Center
        messageLable.textColor = UIColor.redColor()
        messageLable.font = UIFont.systemFontOfSize(13)
        
        UIView.animateWithDuration(2, animations: { () -> Void in
        messageLable.transform = CGAffineTransformMakeTranslation(0, 44)
            
            }) { (_) -> Void in
                UIView.animateWithDuration(2, animations: { () -> Void in
                        messageLable.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        messageLable.removeFromSuperview()
                })
        }
        
    }
    
    
    func changeRootView(){
        
//        tableView.tableFooterView = pullActivity
        tableView.tableFooterView = pullActivity
        if WZUserViewModel.shareInstance.isLogin{
            self.downLoadViewModel = WZDownLoadViewModel()
//          加载数据
            loadData()
            
//            设置自动获取行高
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 200;
            
//            注册cell
            tableView.registerClass(WZHomeTableViewCell.self, forCellReuseIdentifier: WZHomeViewControllerReuserIdenfiter)
            
        }
        
        
        visitor?.setInfo("是达莱桑德罗卡距离首都加拉加斯的", image: "visitordiscover_feed_image_house", animation: true)
    }
    
    //    MARK:懒加载上啦刷新的菊花
    lazy var pullActivity: UIActivityIndicatorView = {
        let pull = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return pull
    }()
    //    MARK:懒加载下拉刷新的refreshView
    
    lazy var refreshView: WZRefreshView = {
    let refreshView = WZRefreshView()
        return refreshView
    
    }()
    //    MARK:数据加载方法
    func loadData(){
        var max_id = 0
        var since_id = 0
        
    since_id = downLoadViewModel?.statesArray.first?.model?.id ?? 0
        if pullActivity.isAnimating(){
            
        max_id = downLoadViewModel?.statesArray.last?.model?.id ?? 0
            
            max_id = max_id - 1
            since_id = 0
        }
        
          //       MARK: 定义闭包回调 回到tableview更新UI
        downLoadViewModel!.loadData(max_id, since_id: since_id) { (isSuccess, count) -> () in
            
//            判断如果上啦刷新的菊花没转就就说明是下拉刷新显示Lable
            if !(self.pullActivity.isAnimating()){
                print(self.pullActivity.isAnimating())
                self.showMessageLable(count)
            }
            
            
            self.pullActivity.stopAnimating()
            
            
            self.tableView.reloadData()
            self.refreshView.endRefresh(isSuccess)
            

            
            
        }
       
    }
}
extension WZHomeController{
   
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return downLoadViewModel?.statesArray.count ?? 0
//        print(downLoadViewModel?.statesArray.count)
        
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(WZHomeViewControllerReuserIdenfiter, forIndexPath: indexPath) as! WZHomeTableViewCell
        

        cell.cellViewModel = downLoadViewModel?.statesArray[indexPath.row]
        
        if indexPath.item == ((downLoadViewModel?.statesArray.count)! - 1) && !(pullActivity.isAnimating()){
            
           pullActivity.startAnimating()
            
            loadData()
            
        }

        return cell
    }
    
}